/* 
Authors:
Vincent Yee 1202853946
Jimmy Nguyen 1202701781

CSE325 Embedded Microprocessor Systems
Spring 2013
Lab Project 1

Vincent Yee: 50%
Jimmy Nguyen: 50%
*/

//#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <cmath>

using namespace std;

typedef int CF_LONGWord;
typedef unsigned char  CF_Byte;
typedef short CF_Word;

void setBit(CF_LONGWord &number, int pos);
void clearBit(CF_LONGWord &number, int pos);
void setBit(CF_LONGWord &number, int pos, bool value);
bool getBit(CF_LONGWord number, int pos);
CF_LONGWord getBits(CF_LONGWord number, int hpos, int lpos);
void setBits(CF_LONGWord &number, int hpos, int lpos, CF_LONGWord value);
CF_Byte getByte(CF_LONGWord number, int pos);
CF_Word getWord(CF_LONGWord number, int pos);
void setByte(CF_LONGWord &number, int pos, CF_Byte value);
void setWord(CF_LONGWord &number, int pos, CF_Word value);

int main()
{
    int num = 358447321; // personal test case
    setWord(num, 1, 58756); // personal test case
    return 0;
}

/* setBit function to set the Bit of a number to 1 */
void setBit(CF_LONGWord &number, int pos) 
{
       int t = pow(2,pos);
       number |=t;
}

/* Clears the bit at position "pos" in CF_LONGWord "number" */
void clearBit(CF_LONGWord &number, int pos)
{
    int t = pow(2,pos);
    int inv = ~t;
    number &=inv;
}

/* setBit function to set the bit to bool value */
void setBit(CF_LONGWord &number, int pos, bool value)
{
    if (value==1)
    {
        setBit(number, pos);
    }
    else
    {
        clearBit(number,pos);
    }
}

/* getBit funciton to get a bit at pos */
bool getBit(CF_LONGWord number, int pos)
{
   number = number >> pos;
   if(number % 2 ==0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

/* getBits in number from hpos to lpos */
CF_LONGWord getBits(CF_LONGWord number, int hpos, int lpos)
{
     int temp=0;
     number = number >> lpos;
     hpos=hpos-lpos;
     while(hpos >= 0)
     {
         temp = temp+(pow(2,hpos));
         hpos=hpos-1;
     }
     number = number & temp;
     return number;
}

/* setBits from hpos to lpos in number to value */
void setBits(CF_LONGWord &number, int hpos, int lpos, CF_LONGWord value)
{
    int mini=lpos;
    for(int i=0; i<=(hpos-mini); i++)
    {
        if (getBit(value, i)==false)
        {
            clearBit(number, lpos);
            lpos++;
        }
        else
        {
            setBit(number, lpos);
            lpos++;
        }
    }
}

/* getByte function to get Byte from number at pos */
CF_Byte getByte(CF_LONGWord number, int pos)
{
    number = number >> (8*pos);
    return (number & 0xFF);
}

/* getWord function to get word from number at pos */ 
CF_Word getWord(CF_LONGWord number, int pos)
{
    number = number >> (16*pos);
    return (number & 0xFFFF);
}

/* setByte function to set bype at pos to value */
void setByte(CF_LONGWord &number, int pos, CF_Byte value)
{
    setBits(number, ((pos*8)+7), (pos*8), value);
}

/* setWord function to set word in number to value at pos */
void setWord(CF_LONGWord &number, int pos, CF_Word value)
{
    setBits(number, ((pos*16)+15), (pos*16), value);
}

